/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include "../include/math.h"

/**
 * @brief Calculates the natural logarithm of a double.
 *
 * This function returns the natural logarithm of the given double value.
 * It returns NAN if the input value is less than or equal to 0.
 *
 * @param x The double value.
 * @return double The natural logarithm of x.
 */
double Slog(double x) {  
    if (x <= 0)
        return DNAN;

    double result = 0.0;  
    double term;  
    double last_result = 0.0;  
    int n = 1;  

    term = (x - 1) / (x + 1);  
    double term_squared = term * term;  

    while (Sabsolute(term) > DEPSILON) {
        result += term / n;  
        term *= term_squared;  
        n += 2;  
    }  
    return 2 * result;  
}
